using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Ltf.Remoting.RemoteEvents;

namespace Ltf.MonitorLizard.Forms
{
	

	public partial class ProgressIndicator : Form, BusyForm
	{
		public event EventHandler Cancel;

		private bool allowCancel;
		private int increment;
		private string newItemText;
		private int extraIncrements;

		public ProgressIndicator(string title, string mainLabelText,
			int max, int increment, bool allowCancel)
		{
			InitializeComponent();
			this.Text = title;
			lblMainLabel.Text = mainLabelText;
			this.allowCancel = allowCancel;
			this.increment = increment;

			btnCancel.Visible = allowCancel;

			pbProgress.Minimum = 0;
			pbProgress.Maximum = max;
			pbProgress.Value = 0;

			this.Show();
			this.BringToFront();
			this.Activate();
			this.Invalidate();
			this.Refresh();
		}

		private void ProgressIndicator_Load(object sender, EventArgs e)
		{

		}

		public void IncrementHandler(object sender, EventArgs e)
		{
			if (e is ProgressIndicatorEventArgs)
			{
				ProgressIndicatorEventArgs piea=(ProgressIndicatorEventArgs) e;
				if (piea.Finished)
				{
					this.Invoke(new EventHandler(CloseForm));
				}
				else
				{
					if (piea.ReScale)
					{
						extraIncrements = piea.ExtraIncrements;
						pbProgress.Invoke(new EventHandler(IncreaseIncrements));
					}
					newItemText = piea.Text;
					lblItemLabel.Invoke(new EventHandler(SetItemText));
					if (cancelButtonPressed)
						piea.Cancel = true;
				}
			}

		}

		public new void Close()
		{
			this.Invoke(new EventHandler(CloseForm));
		}

		private void CloseForm(object Sender, EventArgs e)
		{
			base.Close();
		}

		private void SetItemText(object Sender, EventArgs e)
		{
			pbProgress.Increment(increment);
			lblItemLabel.Text = newItemText;
			this.Refresh();
		}

		private void IncreaseIncrements(object Sender, EventArgs e)
		{
			pbProgress.Maximum+=extraIncrements;
			this.Refresh();
		}


		private void btnCancel_Click(object sender, EventArgs e)
		{
			cancelButtonPressed = true;
			if (Cancel != null)
				Cancel(this, new EventArgs());
		}

		private bool cancelButtonPressed;

		public bool CancelButtonPressed
		{
			get { return cancelButtonPressed; }
			set { cancelButtonPressed = value; }
		}
	
	}
}